package com.twu.biblioteca.ui.menus;

import com.twu.biblioteca.BibliotecaApp;
import com.twu.biblioteca.app.Library;
import com.twu.biblioteca.app.user.User;

import java.io.BufferedReader;
import java.io.IOException;

public class UserLoginMenu {
    public static void displayLoginPrompt() {
        System.out.println("To login please enter your library number:");
    }

    public static User readUserDetailsAndRetrieveUserFromRecords(BufferedReader bufferedReader, Library library) {
        try {
            String libraryNumber = bufferedReader.readLine().trim();
            System.out.println("password: ");
            String password = bufferedReader.readLine().trim();
            User user = library.getUserFromLoginDetails(libraryNumber, password);
            if ( user == null) {
                displayLoginError();
            } else {
                displaySuccessfulLoginMessage(user.getName());
                return user;
            }
        } catch (IOException e) {
            System.out.println("There was an error reading from the provided input reader.");
        }
        return null;
    }

    private static void displaySuccessfulLoginMessage(String username) {
        System.out.println("Welcome " + username + "!");
    }

    private static void displayLoginError() {
        System.out.println("The library number and password provided were not found in the library records.");
    }


}
